package com.tingyuge.thinking.spring.event;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.event.*;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;

/**
 * @author xiaodao
 * @version 1.0
 * @date 2021/12/12
 * @description
 */
@EnableAsync
public class ApplicationListenerDemo implements ApplicationEventPublisherAware {
    public static void main(String[] args) {
//        GenericApplicationContext applicationContext = new GenericApplicationContext();

        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //将引导类作为configuration class
        applicationContext.register(ApplicationListenerDemo.class);
        // 1. 基于spring 接口 向springContext 注册一个事件
        //通过API 注册
        applicationContext.register(MyApplicationListener.class);
        applicationContext.addApplicationListener(new ApplicationListener<ApplicationEvent>() {
            @Override
            public void onApplicationEvent(ApplicationEvent event) {
                println("ApplicationListener-> 接受到 spring 事件 " + event);
            }
        });
        applicationContext.refresh();
        //启动 spring 上下文
        applicationContext.start();
        applicationContext.close();
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        applicationEventPublisher.publishEvent(new ApplicationEvent(" hello,world") {
        });
        //playLoad事件
        applicationEventPublisher.publishEvent("hello-world");
    }

    static class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent>{
        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            println("MyApplicationListener-> 接受到 spring 事件 " + event);
        }
    }

    @EventListener
    @Order(1)
    public void onApplicationEvent(ContextRefreshedEvent event) {
        println(" @EventListener-> (onApplicationEvent) 接受到 ContextRefreshedEvent 事件 ");
    }

    @Order(2)
    @EventListener
    public void onApplicationEvent1(ContextRefreshedEvent event) {
        println(" @EventListener-> (onApplicationEvent1) 接受到 ContextRefreshedEvent 事件 ");
    }

    @EventListener
    @Async
    public void onApplicationEvent(ContextStartedEvent event) {
        println(" @EventListener-> 接受到 ContextStartedEvent 事件 ");
    }

    @EventListener
    public void onApplicationEvent(ContextClosedEvent event) {
        println(" @EventListener-> 接受到 ContextClosedEvent 事件 ");
    }

    private static void println(Object printable) {
        System.out.printf("[线程 %s]: %s \n", Thread.currentThread().getName(), printable);
    }
}
